package com.mybats.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils {

    public static SqlSession getSqlSession(){
        InputStream resourceAsStream = null;
        SqlSession sqlSession = null;

        try {
            //第一步通过resouce去加载配置文件
            resourceAsStream = Resources.getResourceAsStream("mybatis/mybatisConfig.xml");
            //通过SqlSessionFactoryBuilder 创建一个工厂
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            //然后从工厂里面去打开sesstion
            //自动提交事务
            sqlSession = build.openSession(true);
            //sqlSession = build.openSession();

        } catch (IOException e) {
            e.printStackTrace();
        }

        return sqlSession;

    }
}
